package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import com.sinosoft.hydra.um.condition.SearchEBomCondition;
import com.sinosoft.hydra.um.domain.permission.TEBom;
import com.sinosoft.hydra.um.domain.permission.TPBom;
import cn.com.sinux.spring.exception.BusinessException;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.FileService;
import com.sinosoft.hydra.um.services.SysEBomService;
import com.sinosoft.hydra.um.services.SysPBomService;
import cn.com.sinux.spring.utils.PageUtils;
import cn.com.sinux.spring.utils.ValidatorUtils;;
import com.sinosoft.web.common.WebInput;
import cn.com.sinux.spring.json.JsonFilter;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 装配结构API
 */
@Controller
@RequestMapping("/api/ebom")
public class SysEBomApi {

    @Autowired
    private SysPBomService sysPBomService;
    @Autowired
    private SysEBomService sysEBomService;
    @Autowired
    private FileService fileService;


    /**
     * 到EBOM首页面
     *
     * @return
     */
    @RequestMapping("index")
    @RequiresPermissions("ebom:data:list")
    public String index() {
        LoggerHelper.logInfo("访问EBom结构列表");
        return "hydra_easyui/ebom/index";
    }

    /**
     * 到ebom组装页面
     *
     * @param searchEBomCondition
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    @RequiresPermissions("ebom:data:list")
    @ResponseBody
    public Object list(SearchEBomCondition searchEBomCondition,
                       @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNumber,
                       @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize
    ) {
        Pageable pageable = PageUtils.buildPageRequest(pageNumber, pageSize, "createTime", "desc");
        Page<TEBom> teBomPage = sysEBomService.findEBoms(searchEBomCondition, pageable);
        return JsonFilter.doPropertyFilter(teBomPage, new PropertyCollection()
                .addProperties(TEBom.class, "id", "code", "eName", "description", "bomType", "cover", "createTime", "state", "doProcess", "pBomDes"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("view")
    @RequiresPermissions("ebom:data:list")
    public String view(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("id", id);
        return "hydra_easyui/ebom/view";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("children")
    @RequiresPermissions("ebom:data:list")
    @ResponseBody
    public Object list(@RequestParam(value = "id", required = true) String id) {
        List<TEBom> page = sysEBomService.findChildren(id);
        return JsonFilter.doPropertyFilter(page, new PropertyCollection()
                .addProperties(TEBom.class, "id", "code", "eName", "description", "bomType", "cover", "createTime", "state", "bomGroup", "doProcess", "pBomDes"));
    }

    /**
     * 到EBOM添加页面
     *
     * @param curId
     * @param model
     * @return
     */
    @RequestMapping("pBomDetailList")
    public String toSuitDetail(@RequestParam(value = "curId", required = true) String curId,
                               @RequestParam(value = "parentId", required = true) String parentId, Model model) {
        TEBom teBom = sysEBomService.findOne(curId);
        model.addAttribute("pbomId", teBom.gettPBom().getId());
        model.addAttribute("parentId", parentId);
        //计算现有PBOM节点数
        return "hydra_easyui/ebom/pBomDetailList";
    }

    /**
     * 到EBOM添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("toSuit")
    public String toSuit(@RequestParam(value = "id", required = true) String id, Model model) {
        TPBom tpBom = sysPBomService.findOne(id);
        model.addAttribute("pBom", tpBom);
        //计算现有PBOM节点数
        return "hydra_easyui/ebom/suitEBom";
    }

    /**
     * 到EBOM添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("toPBomDetail")
    @RequiresPermissions(value = {"ebom:data:view", "ebom:data:list"}, logical= Logical.OR)
    public String toPBomDetail(@RequestParam(value = "id", required = true) String id, Model model) {
        TPBom tpBom = sysPBomService.findOne(id);
        model.addAttribute("pBom", tpBom);
        //计算现有PBOM节点数
        return "hydra_easyui/ebom/pBomDetail";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("choose")
//    @RequiresPermissions("ebom:data:add")
    public String choose(Model model, SearchEBomCondition searchEBomCondition) {
        model.addAttribute("condition", searchEBomCondition);
        return "hydra_easyui/ebom/choose";
    }

    /**i
     * 到EBOM添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("build")
    @RequiresPermissions("ebom:data:add")
    public String build(@RequestParam(value = "id", required = true) String id, Model model) {
        //计算现有PBOM节点数
        TPBom tpBom = sysPBomService.findOne(id);
        model.addAttribute("pBom", tpBom);
        model.addAttribute("hasChildren", ValidatorUtils.isArrayNotEmpty(tpBom.getTpBomRelations()));
        //计算现有PBOM节点数
        return "hydra_easyui/ebom/build";
    }



    /**i
     * 到EBOM添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("toAdd")
    @RequiresPermissions("ebom:data:add")
    public String toAdd(Model model) {
        return "hydra_easyui/ebom/add";
    }

    /**
     * 到EBOM添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("toSearch")
    public String toSearch(Model model) {
        //计算现有PBOM节点数
        return "hydra_easyui/ebom/search";
    }

    /**
     * 添加ebom
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("ebom:data:add")
    public Object add(SearchEBomCondition eBomCondition,
                      @RequestParam(value = "number", required = true) int number, Model model) {
        List<TEBom> teBoms = sysEBomService.addEBom(eBomCondition, number, eBomCondition.geteBomMakes());
        LoggerHelper.logInfo("添加了EBom结构", new LoggerHelper.PropertyReader<TEBom>(teBoms, "eName"));
        return JsonFilter.doPropertyFilter(teBoms, new PropertyCollection()
                .addProperties(TEBom.class, "code"));
    }

    /**
     * 回收
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "recycle", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("ebom:data:recycle")
    public Object recycle(@RequestParam(value = "id", required = true) String id) {
        TEBom teBom = sysEBomService.recycle(id);
        LoggerHelper.logInfo("回收了EBom结构", teBom.geteName());
        return JsonFilter.doPropertyFilter(teBom, new PropertyCollection()
                .addProperties(TEBom.class, "code"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("export")
    @RequiresPermissions("ebom:data:export")
    public void export(
            WebInput in, HttpServletResponse response,
            @RequestParam(value = "id", required = true) String id) {
        try {
            TEBom pbom = sysEBomService.findOne(id);
            byte[] bytes = sysEBomService.doExportEBom(id);
            LoggerHelper.logInfo("导出了EBom“{}”结构数据", pbom.geteName());
            in.getWebOutput(response).sendFile(bytes, pbom.geteName() + "的EBom结构.xls", null, true, 30, true, false);
        } catch (IOException ex) {
            throw new BusinessException("结构导出失败");
        }
    }

    /**
     * 导出工件的所有信息到文件包
     *
     * @param in
     * @param id
     * @return
     */
    @RequestMapping("packageEBOM")
    @RequiresPermissions("ebom:all:export")
    public void exportAll(
            WebInput in, HttpServletResponse response,
            @RequestParam(value = "id", required = true) String id) {
        try {
            TEBom tpBom = sysEBomService.findOne(id);
            String path = sysEBomService.doExportAllEBomInfoToWord(id);
            LoggerHelper.logInfo("导出了EBom“{}”结构数据", tpBom.geteName());
            in.getWebOutput(response).sendFile(fileService.getRelativePath() + path, tpBom.geteName() + "的数据包.zip");
        } catch (IOException ex) {
            throw new BusinessException("结构导出失败");
        }
    }
}
